{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%%\n",
    "from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting\n",
    "from vnpy.app.cta_strategy.strategies.(策略名) import (\n",
    "    策略类,\n",
    ")#将策略放置到vnpy官方文件夹内的cta_strategy才可以调用该策略，不是放在本地用户文件夹内\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%%\n",
    "engine = BacktestingEngine()\n",
    "engine.set_parameters(\n",
    "    vt_symbol=\"XBTUSD.BITMEX\",\n",
    "    interval=\"1m\",\n",
    "    start=datetime(2016, 12, 1),\n",
    "    end=datetime(2019, 4, 30),\n",
    "    rate=3.5/10000,\n",
    "    slippage=0.5,\n",
    "    size=1,\n",
    "    pricetick=0.5,\n",
    "    capital=100000,\n",
    "    inverse=False #正反向合约\n",
    ")\n",
    "engine.add_strategy(策略类, {})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%%\n",
    "engine.load_data()\n",
    "engine.run_backtesting()\n",
    "df = engine.calculate_result()\n",
    "engine.calculate_statistics()\n",
    "engine.show_chart()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "setting = OptimizationSetting()\n",
    "#\"sharpe_ratio , total_return , return_drawdown_ratio\" #自己需要自由度跑的结果在回测引擎的cta_strategy的back_testing.py文件里查找\n",
    "setting.set_target(\"total_return\")\n",
    "setting.add_parameter(\"\", 14, 18, 1)\n",
    "setting.add_parameter(\"\", 8, 10, 1)\n",
    "setting.add_parameter(\"\", 12, 14, 1)\n",
    "setting.add_parameter(\"\", 16, 32, 2)\n",
    "setting.add_parameter(\"\", 7, 11, 1)\n",
    "setting.add_parameter(\"\", 1, 3, 1)\n",
    "setting.add_parameter(\"atr_window\", 1, 3, 1)\n",
    "#setting.add_parameter(\"risk_level\", 0.2, 0.2, 0.1)\n",
    "setting.add_parameter(\"trailing_tax\", 0.7, 2.5, 0.1)\n",
    "\n",
    "\n",
    "engine.run_ga_optimization(setting)#遗传算法\n",
    "engine.run_optimization(setting)#多进程算法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
